package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetMealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.SetMeal;
import com.itheima.reggie.entity.SetMealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetmealMapper, SetMeal> implements SetMealService {
    @Autowired
    SetMealDishService setMealDishService;
    @Lazy
    @Autowired
    CategoryService categoryService;


    @Override
    public List<SetMealDish> getId(Long id) {
        LambdaQueryWrapper<SetMealDish> query = new LambdaQueryWrapper<>();
        query.eq(SetMealDish::getSetmealId,id);
        return setMealDishService.list(query);
    }

    @Override
    public List<SetMealDto> list(Long id, Integer status) {
        LambdaQueryWrapper<SetMeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetMeal::getCategoryId,id);
        lqw.eq(status!= null,SetMeal::getStatus,status);
        List<SetMeal> setMeals = getBaseMapper().selectList(lqw);
        return setMeals.stream().map(SetMeal -> {
            SetMealDto SetMealDto = new SetMealDto();
            BeanUtils.copyProperties(SetMeal, SetMealDto);
            LambdaQueryWrapper<SetMealDish> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(SetMealDish::getSetmealId,SetMeal.getId());
            SetMealDto.setSetmealDishes(setMealDishService.list(lqw1));
            return SetMealDto;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean delete(List<Long> ids) {
        List<SetMeal> dishes = getBaseMapper().selectBatchIds(ids);
        for (SetMeal SetMeal : dishes) {
            if(SetMeal.getStatus()==1){
                throw new CustomException("该套餐正在出售，请先下架再进行删除");
            }
        }
        int i = getBaseMapper().deleteBatchIds(ids);
        return i>=1;
    }

    @Override
    public boolean update(SetMealDto dto) {
        int i = getBaseMapper().updateById(dto);
        LambdaQueryWrapper<SetMealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetMealDish::getSetmealId,dto.getId());
        //删除原有的套餐菜品内容
        setMealDishService.remove(lqw);
        //添加现有的菜品套餐内容
        List<SetMealDish> collect = dto.getSetmealDishes().stream().map(std -> {
            std.setSetmealId(dto.getId());
            return std;
        }).collect(Collectors.toList());
        boolean b = setMealDishService.saveBatch(collect);
        return i>= 1 && b;
    }

    @Override
    public boolean add(SetMealDto dto) {
        //添加套餐信息
        int insert = getBaseMapper().insert(dto);

        //添加套餐菜品关联表信息
        List<SetMealDish> collect = dto.getSetmealDishes().stream().map(std -> {
            std.setSetmealId(dto.getId());
            return std;
        }).collect(Collectors.toList());
        boolean b = setMealDishService.saveBatch(collect);
        return insert>=1&&b;
    }

    @Override
    public IPage<SetMealDto> pageAll(PageParam pageParam) {
        LambdaQueryWrapper<SetMeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(pageParam.getName() != null, SetMeal::getName, pageParam.getName());
        IPage<SetMeal> setMealPage = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        IPage<SetMeal> selectPage = getBaseMapper().selectPage(setMealPage, lqw);
        IPage<SetMealDto> sd = new Page<>();
        sd.setTotal(selectPage.getTotal());
        List<SetMealDto> collect = selectPage.getRecords().stream().map(records -> {
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(records, setMealDto);
            setMealDto.setCategoryName(categoryService.getById(records.getCategoryId()).getName());
            LambdaQueryWrapper<SetMealDish> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(SetMealDish::getSetmealId, setMealDto.getId());
            setMealDto.setSetmealDishes(setMealDishService.list(lqw2));
            return setMealDto;
        }).collect(Collectors.toList());
        sd.setRecords(collect);
        return sd;

    }
}
